package com.atguigu.gulimail.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gulimail.product.entity.CategoryEntity;
import com.atguigu.gulimail.product.service.CategoryService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Response;


/**
 * 商品三级分类
 *
 * @author fk
 * @email hahahahha@gmail.com
 * @date 2020-08-09 23:15:28
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查出所有分类以及子分类,以树形结构组装起来
     */
    @RequestMapping("/list/tree")
    public Response list () {
        List<CategoryEntity> entities = categoryService.listWithTree();
        return Response.ok().put("data", entities);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:category:list")
    public Response list (@RequestParam Map<String, Object> params) {
        PageUtils page = categoryService.queryPage(params);
        return Response.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    // @RequiresPermissions("product:category:info")
    public Response info (@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);

        return Response.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //  @RequiresPermissions("product:category:save")
    public Response save (@RequestBody CategoryEntity category) {
        categoryService.save(category);
        return Response.ok();
    }

    /**
     * 批量修改
     */
    @RequestMapping("/update/sort")
    // @RequiresPermissions("product:category:update")
    public Response updateSort (@RequestBody CategoryEntity[] category) {
        categoryService.updateBatchById(Arrays.asList(category));
        return Response.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:category:update")
    public Response update (@RequestBody CategoryEntity category) {
        categoryService.updateCascade(category);
        return Response.ok();
    }

    /**
     * 删除
     *
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:category:delete")
    public Response delete (@RequestBody Long[] catIds) {
        categoryService.removeByMenuIds(Arrays.asList(catIds));
        return Response.ok();
    }

}
